<?php

namespace common\models\info;

use common\component\BaseActiveRecord;
use common\component\Constant;
use common\utils\CommonUtil;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%zt_mood}}".
 *
 *  @property int $id
 *  @property int $mark_content //内容
 * @property int $status 状态(0未审核 1通过)
 * @property int $updated_at 修改时间
 *  @property string $created_at 创建时间
 *  @property string $content 内容
 */
class Mood extends BaseActiveRecord
{
    const STATUS_DISABLE = 0;   //禁用
    const STATUS_ENABLE = 1;    //启用

    static $statusArr = [
        self::STATUS_ENABLE => '启用',
        self::STATUS_DISABLE => '禁用',
    ];
    public static $statusDropdownList = [
        ['id'=>'','text'=>"请选择文章状态"],
        ['id'=>self::STATUS_DISABLE,'text'=>"禁用"],
        ['id'=>self::STATUS_ENABLE,'text'=>"启用"],
    ];
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{zt_mood}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [[ 'created_at','updated_at','status'], 'integer'],
            [['content','mark_content'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '序列',
            'content' => '内容',
            'mark_content'=>'mark内容',
            'created_at' => '创建时间',
            'status'=>'状态',
            'updated_at' => '更新时间',
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    self::EVENT_BEFORE_INSERT => ['created_at'],
                    self::EVENT_BEFORE_UPDATE => ['updated_at']
                ]
            ]
        ];
    }

    /**
     * 保存数据之前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if (!parent::beforeSave($insert)) {
            return false;
        }
        if ($insert) {
            $this->updated_at = $this->created_at;
        }
        return true;
    }
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
    }


    public function afterFind()
    {
        parent::afterFind();
        $this->ui();
    }
   public function ui(){
       $this->ui["status"] =$this->status==''?'':self::$statusArr[$this->status];
       $this->ui["updated_at"] = date("Y-m-d H:i:s",$this->updated_at);
       $this->ui["created_at"] = date("Y-m-d H:i:s",$this->created_at);
   }
    /**
     * 实例化模型对像
     * @param $id
     * @return BaseActiveRecord|null|static
     */
    public static function modelById($id) {
        if (!$id) {
            $model = new self();
        }
        else {
            $model = static::findOne($id);
        }
        return $model;
    }
    public static function getMood($page,$pageSize){
        $key = "mood";
        $data = CommonUtil::getCache(__CLASS__,$key);
        if(!$data){
            $q = static::find()->select("id,content,created_at")->orderBy("created_at DESC");
            $data = self::arrListByPage($q,$page,$pageSize);
            CommonUtil::setCache(__CLASS__,$key,$data,3600);
        }
        return $data;

    }

    public function dataListTable($post) {
        $where = ["and"];
        if (isset($post['status']) && $post['status'] != '') {
            $where[] =['=','status',$post['status']];
        }
        $query = self::find()->where($where);
        $dataArr = self::table($query,$post);
        return $dataArr;
    }
}
